package com.zdkj.subway.check.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.check.entity.SwAuConductorToBeChecked;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuConductorToBeCheckedMapper;
import com.zdkj.subway.check.mapper.SysUserMapper;
import com.zdkj.subway.check.result.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
/**
 * 线网层级确认-数据协查管理-BOM票款差异协查-售票员票款差异协查
 * 线网层级确认-数据协查管理-BOM票款差异协查-BOM系统差异协查-BOM系统数据
 * 售票员待协查 `
 */
@Service
public class SwAuConductorToBeCheckedService {

    @Resource
    private SwAuConductorToBeCheckedMapper swAuConductorToBeCheckedMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 售票员待协查 列表
     * @param jsonObject
     * @return
     */
    public Result getSwAuConductorNoticeList(JSONObject jsonObject) {

        //分页
        Integer startPage = jsonObject.getInteger("currentPage");
        Integer pageSize = jsonObject.getInteger("pageSize");
        //通知时间
        String assistTime = jsonObject.getString("assistTime");
        //收入日期
        String recordTime = jsonObject.getString("recordTime");
        Date startTime = jsonObject.getDate("startTime");
        Date endTime = jsonObject.getDate("endTime");

        //差异金额开始结束
        BigDecimal num = new BigDecimal("100");
        BigDecimal replyErrorReturnMoneyStart = jsonObject.getBigDecimal("replyErrorReturnMoneyStart");
        BigDecimal replyErrorReturnMoneyEnd = jsonObject.getBigDecimal("replyErrorReturnMoneyEnd");

        //售票员工号
        String conductorPersonCode = jsonObject.getString("conductorPersonCode");

        //设备操作号
        String equipmentNumber = jsonObject.getString("equipmentNumber");

        //协查状态
        String assistState = jsonObject.getString("assistState");
        String assistStatess = jsonObject.getString("assistStatess");
        String kyAssistInvestStatus = jsonObject.getString("kyAssistInvestStatus");
        String kyAssistInvestStatusss = jsonObject.getString("kyAssistInvestStatusss");
        String jdReplyReason = jsonObject.getString("jdReplyReason");
        String jdReplySatus = jsonObject.getString("jdReplySatus");
        String kyReplySatus = jsonObject.getString("kyReplySatus");
        String lineCode = jsonObject.getString("lineCode");
        String stationCode = jsonObject.getString("stationCode");
        String replaceStatus = jsonObject.getString("replaceStatus");

        Example example = new Example(SwAuConductorToBeChecked.class);
        Example.Criteria criteria = example.createCriteria();
        /*if(startTime != null && endTime != null) {
            criteria.andBetween("assistTime", startTime, endTime);
        }*/
        if (replyErrorReturnMoneyStart != null && replyErrorReturnMoneyEnd != null) {
            replyErrorReturnMoneyStart = replyErrorReturnMoneyStart.multiply(num);
            replyErrorReturnMoneyEnd = replyErrorReturnMoneyEnd.multiply(num);
            criteria.andBetween("diffMoney", replyErrorReturnMoneyStart, replyErrorReturnMoneyEnd);
        }
        if (!StringUtils.isEmpty(conductorPersonCode)) {
            criteria.andEqualTo("conductorPersonCode", conductorPersonCode);
        }
        if (!StringUtils.isEmpty(equipmentNumber)) {
            criteria.andEqualTo("equipmentNumber", equipmentNumber);
        }
        if (!StringUtils.isEmpty(assistState)) {
            criteria.andEqualTo("assistState", assistState);
        }
        if (!StringUtils.isEmpty(assistStatess)) {
            List assistStatessList = Arrays.asList(assistStatess.split(","));//根据逗号分隔转化为list
            criteria.andIn("assistState", assistStatessList);
        }
        if (!StringUtils.isEmpty(kyAssistInvestStatus)) {
            criteria.andEqualTo("kyAssistInvestStatus", kyAssistInvestStatus);
        }
        if (!StringUtils.isEmpty(kyAssistInvestStatusss)) {
            List kyAssistInvestStatusList = Arrays.asList(kyAssistInvestStatusss.split(","));//根据逗号分隔转化为list
            criteria.andIn("kyAssistInvestStatus", kyAssistInvestStatusList);
        }
        if (!StringUtils.isEmpty(replaceStatus)) {
            criteria.andEqualTo("replaceStatus", replaceStatus);
        }
        if (!StringUtils.isEmpty(jdReplyReason)) {
            criteria.andEqualTo("jdReplyReason", jdReplyReason);
        }
        if (!StringUtils.isEmpty(jdReplySatus)) {
            criteria.andEqualTo("jdReplySatus", jdReplySatus);
        }
        if (!StringUtils.isEmpty(kyReplySatus)) {
            criteria.andEqualTo("kyReplySatus", kyReplySatus);
        }
        if (!StringUtils.isEmpty(lineCode)) {
            criteria.andEqualTo("lineCode", lineCode);
        }
        if (!StringUtils.isEmpty(stationCode)) {
            criteria.andEqualTo("stationCode", stationCode);
        }
        example.setOrderByClause("ASSIST_TIME desc,CREATE_TIME desc");
        if(StringUtils.isEmpty(lineCode)) {
            PigxUser user = SecurityUtils.getUser();
            Map<String, Object> p1 = new HashMap<>();
            p1.put("user_code", user.getUserCode());
            List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
            SysUser sysUser = sysUsers.get(0);
            String managementLineStation = sysUser.getManagementLineStation();
            //管辖线路
            if (!StringUtils.isEmpty(managementLineStation)) {
                JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
                List<String> LineObjects = new ArrayList<>();
                for (int i = 0; i < managementLineStations.size(); i++) {
                    JSONObject jsonObject1 = managementLineStations.getJSONObject(i);
                    lineCode = jsonObject1.getString("line");
                    LineObjects.add(lineCode);//管辖多个线路
                }
                criteria.andIn("lineCode", LineObjects);
            }else {
                //不管辖线路
                criteria.andEqualTo("lineCode", sysUser.getLineCode());
            }
        }
        if (null != assistTime) {
            criteria.andLike("assistTime", assistTime+"%");
        }
        if (!StringUtils.isEmpty(recordTime)) {
            criteria.andEqualTo("recordTime", recordTime);
        }
        criteria.andEqualTo("delFlag", "0");
        JSONObject resultJson = new JSONObject();
        if(startPage!=null){
            PageHelper.startPage(startPage,pageSize);
        }
        List<SwAuConductorToBeChecked> swAuConductorToBeCheckeds
                = swAuConductorToBeCheckedMapper.selectByExample(example);
        if(startPage!=null){
            PageInfo<SwAuConductorToBeChecked> swAuConductorToBeCheckedPageInfo = new PageInfo<>(swAuConductorToBeCheckeds);
            resultJson.put("pageInfo", swAuConductorToBeCheckedPageInfo);
        }
        resultJson.put("list", swAuConductorToBeCheckeds);
        return new Result(ResultDetailMsg.OK, resultJson);
    }

    /**
     * 售票员待协查 单独查询
     * @param jsonObject
     * @return
     */
    public Result getSwAuConductorNotice(JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        SwAuConductorToBeChecked swAuConductorToBeChecked = swAuConductorToBeCheckedMapper.selectByPrimaryKey(id);
        JSONObject resultJson = new JSONObject();
        resultJson.put("obj",swAuConductorToBeChecked);
        return new Result(ResultDetailMsg.OK,resultJson);
    }

    public Result updateSwAuConductorNotice(JSONObject jsonObject) {
        SwAuConductorToBeChecked swAuConductorToBeChecked =
                JSONObject.parseObject(jsonObject.toJSONString(), SwAuConductorToBeChecked.class);
        int i = swAuConductorToBeCheckedMapper.updateByPrimaryKeySelective(swAuConductorToBeChecked);
        return new Result(ResultDetailMsg.OK);
    }
}
